package com.example.ball;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;

public class MainActivity extends AppCompatActivity {

    float zoom = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ballClass myBall = findViewById(R.id.myBall);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ballClass myBall = findViewById(R.id.myBall);
        if (myBall == null) {
            return super.onTouchEvent(event);
        }

        switch (event.getAction() & MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_POINTER_DOWN:
                zoom = calculateZoom(event);
                break;
            case MotionEvent.ACTION_MOVE:
                zoom = calculateZoom(event);
                myBall.setScaleX(zoom);
                myBall.setScaleY(zoom);
                break;
        }

        return true;
    }

    // 计算缩放比例
    private float calculateZoom(MotionEvent event) {
        if (event.getPointerCount() < 2) {
            return zoom;
        }

        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        float newDistance = (float) Math.sqrt(x * x + y * y);
        return newDistance / 50;
    }
}
